{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<p align=\"center\">\n",
    "    <img src=\"https://github.com/GeostatsGuy/GeostatsPy/blob/master/TCG_color_logo.png?raw=true\" width=\"220\" height=\"240\" />\n",
    "\n",
    "</p>\n",
    "\n",
    "## Data Analytics Experiential Learning \n",
    "\n",
    "### Uncertainty Modeling with Bootstrap and Monte Carlo Simulation\n",
    "\n",
    "\n",
    "#### Michael Pyrcz, Associate Professor, University of Texas at Austin \n",
    "\n",
    "##### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise: Bootstrap for Subsurface Data Analytics in Python \n",
    "\n",
    "Let's make a simple bootstrap and Monte Carlo Simulation workflow to calculate an uncertainty model.\n",
    "\n",
    "Here's some lectures that may be helpful:\n",
    "\n",
    "* [Monte Carlo Simulation](https://youtu.be/Qb8TsSINpnU)\n",
    "\n",
    "* [Bootstrap](https://youtu.be/wCgdoImlLY0)\n",
    "\n",
    "#### The Problem: What is the uncertainty in Oil in Place?\n",
    "\n",
    "We have 25 wells drilled in the reservoir with porosity and thickness information. The file is available here: [well_data](https://github.com/GeostatsGuy/GeoDataSets/blob/master/WellPorThick.csv).\n",
    "\n",
    "Build an uncertainty model for oil in place. Provide the distribuion labelled with P10, P50 and P90.\n",
    "\n",
    "#### Workflow Steps\n",
    "\n",
    "1. Calculate the uncertainty in average porosity ($\\phi$) and thickness ($th$) by bootstrap.\n",
    "\n",
    "2. Assume known/constant reservoir area ($a$) = $1,000,000 m^2$ and oil saturation ($s_o$) = $1.0$\n",
    "\n",
    "3. Apply the OIL in place transfer function with the bootstrap and constant inputs. The transfer function is:\n",
    "\n",
    "\\begin{equation}\n",
    "OIP = \\overline{\\phi} \\cdot \\overline{th} \\cdot a \\cdot s_o\n",
    "\\end{equation}\n",
    "\n",
    "4. Visualize and summarize the results, include distribution with P10, P50 and P90 labelled.\n",
    "\n",
    "#### Objective \n",
    "\n",
    "To provide a realistic, experiential learning opportunity. \n",
    "\n",
    "#### Import Packages\n",
    "\n",
    "We will also need some standard packages. These should have been installed with Anaconda 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np                        # ndarrys for gridded data\n",
    "import pandas as pd                       # DataFrames for tabular data\n",
    "import matplotlib.pyplot as plt           # for plotting\n",
    "from inspect import signature             # find number of arguments in a function\n",
    "import random                             # random sampling"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Declare Functions and Provide Code Snipets\n",
    "\n",
    "Declare convenience functions and code snipets to help with the workflow construction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load Data\n",
    "#DataFrame = pd.read_csv('FileName.csv')\n",
    "# E.g.: df = pd.read_csv('https://raw.githubusercontent.com/GeostatsGuy/GeoDataSets/master/WellPorThick.csv')\n",
    "\n",
    "# Extract Column as a 1D Array\n",
    "# 1d_ndarray = df['Feature_Name']\n",
    "# E.g.: porosity = df['Por'].values\n",
    "\n",
    "# Bootstrap to calcuate uncertainty in a statistic\n",
    "def bootstrap(zdata,nreal,stat):\n",
    "    zreal = []                            # declare an empty list to store the bootstrap realizations\n",
    "    for l in range(0,nreal):              # loop over the L bootstrap realizations\n",
    "        samples = random.choices(zdata, k=len(zdata)) # n Monte Carlo simulations, sample with replacement\n",
    "        zreal.append(stat(samples))       # calculate the realization of the statistic and append to list\n",
    "    return np.array(zreal)                          # return the list of realizations of the statistic\n",
    "# 1d_ndarray = bootstrap(1d_ndarray,L,statistic)\n",
    "# E.g.: por_avg_real = bootstrap(por,L,np.average)\n",
    "\n",
    "# Make Constant Inputs, Arrary of Constants if No Uncertainty\n",
    "# 1d_ndarray = np.full(number_realizations,value)\n",
    "# E.g.: area = np.full(number_realizations,value)\n",
    "\n",
    "# Combine Bootstrap Inputs into 1 Array\n",
    "# 2d_ndarray = np.hstack((1d_ndarray,1d_ndarray,...,1d_ndarray)) \n",
    "# E.g.: inputs = np.hstack((boot_avg_por,boot_avg_thick,area,so))\n",
    "\n",
    "# Tranfer function as a function\n",
    "def OIP(avg_por,avg_thick,area,so):\n",
    "    return avg_por*avg_thick*area*so\n",
    "\n",
    "# Monte Carlo Simulation with input realizations and transfer function\n",
    "def MCS(transfer_function, inputs):\n",
    "    sig = signature(transfer_function)\n",
    "    ninput = len(sig.parameters) \n",
    "    nreal = inputs.shape[0]\n",
    "    if ninput != inputs.shape[1]:\n",
    "        print('Function does not agree with the number of inputs!')\n",
    "        return 0\n",
    "    realizations = []\n",
    "    for i in range(0,nreal):\n",
    "        realizations.append(transfer_function( *(inputs[i].tolist()) ) )\n",
    "    return np.array(realizations)\n",
    "# 1d_ndarray = MCS(function,2d_ndarray)        # number of inputs to function equal number of columns for inputs\n",
    "# decision_criteria = MCS(transfer_function=OIP,inputs = x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Insert Your Workflow Below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Determine the number of realizations\n",
    "L = 1000\n",
    "\n",
    "# Load and extract the well data\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Comments\n",
    "\n",
    "This was a basic problem for uncertainty modeling with bootstrap and Monte Carlos simulation. Much more can be done!\n",
    "  \n",
    "I hope this was helpful,\n",
    "\n",
    "*Michael*\n",
    "\n",
    "#### The Author:\n",
    "\n",
    "### Michael Pyrcz, Associate Professor, University of Texas at Austin \n",
    "*Novel Data Analytics, Geostatistics and Machine Learning Subsurface Solutions*\n",
    "\n",
    "With over 17 years of experience in subsurface consulting, research and development, Michael has returned to academia driven by his passion for teaching and enthusiasm for enhancing engineers' and geoscientists' impact in subsurface resource development. \n",
    "\n",
    "For more about Michael check out these links:\n",
    "\n",
    "#### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n",
    "\n",
    "#### Want to Work Together?\n",
    "\n",
    "I hope this content is helpful to those that want to learn more about subsurface modeling, data analytics and machine learning. Students and working professionals are welcome to participate.\n",
    "\n",
    "* Want to invite me to visit your company for training, mentoring, project review, workflow design and / or consulting? I'd be happy to drop by and work with you! \n",
    "\n",
    "* Interested in partnering, supporting my graduate student research or my Subsurface Data Analytics and Machine Learning consortium (co-PIs including Profs. Foster, Torres-Verdin and van Oort)? My research combines data analytics, stochastic modeling and machine learning theory with practice to develop novel methods and workflows to add value. We are solving challenging subsurface problems!\n",
    "\n",
    "* I can be reached at mpyrcz@austin.utexas.edu.\n",
    "\n",
    "I'm always happy to discuss,\n",
    "\n",
    "*Michael*\n",
    "\n",
    "Michael Pyrcz, Ph.D., P.Eng. Associate Professor The Hildebrand Department of Petroleum and Geosystems Engineering, Bureau of Economic Geology, The Jackson School of Geosciences, The University of Texas at Austin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
